package com.chengcang.api.modules.busi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chengcang.api.common.annotation.Login;
import com.chengcang.api.modules.busi.dto.BusiOrderDeliverDTO;
import com.chengcang.api.modules.busi.dto.BusiOrderPageDTO;
import com.chengcang.api.modules.busi.dto.BusiOrderRequestDTO;
import com.chengcang.api.modules.busi.service.BusiOrderService;
import com.chengcang.api.modules.busi.vo.BusiOrderDetailVo;
import com.chengcang.api.modules.busi.vo.BusiOrderVO;
import com.chengcang.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *
 * 2.23 订单需求变更：
 * 1.对于转单操作，增加了甲方id的字段，甲方id: 创建需求方id
 *
 * 2.需求方id & 甲方id(pa_id):
 *      需求方id: 是需求的root
 *      甲方: 创建订单，或转单方
 *
 * 3. app 页面展示：
 *      首页: 公司调履约订单，supply接口
 *      我的: ALL(创建的需求，转的订单，创建的订单) - 创建的订单(履约订单)， demand_id
 *
 *
 */

/**
 * @author zhanli
 * @since 1.0.0 2023-01-10
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单模块")
public class ApiOrderController {
    @Autowired
    private BusiOrderService busiOrderService;


    @Login
    @GetMapping("/supply/page")
    @ApiOperation("订单履约分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
    })
    public Result<Page<BusiOrderVO>> supplyPage(BusiOrderPageDTO busiOrderPageDto) {
        return new Result<Page<BusiOrderVO>>().ok(busiOrderService.getSupplyPage(busiOrderPageDto));
    }

    @Login
    @GetMapping("/demand/page")
    @ApiOperation("供应方支付订单分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
    })
    public Result<Page<BusiOrderVO>> demandPage(BusiOrderPageDTO busiOrderPageDto) {
        return new Result<Page<BusiOrderVO>>().ok(busiOrderService.getDemandOrderPage(busiOrderPageDto));
    }

    @Login
    @GetMapping("/all/page")
    @ApiOperation("公司全部订单(履约支付)分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
    })
    public Result<Page<BusiOrderVO>> allPage(BusiOrderPageDTO busiOrderPageDto) {
        return new Result<Page<BusiOrderVO>>().ok(busiOrderService.getAllOrderPage(busiOrderPageDto));
    }


    @Login
    @ApiOperation("订单未履约量")
    @GetMapping("/supply/unsuppliedNum")
    public Result<Long> unsuppliedNum() {
        return new Result<Long>().ok(busiOrderService.getUnsuppliedNum());
    }

    @Login
    @PostMapping
    @ApiOperation("保存")
    public Result<Object> save(@RequestBody @Validated BusiOrderRequestDTO dto){
        busiOrderService.saveOrder(dto);
        return new Result<Object>();
    }

    @Login
    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<BusiOrderDetailVo> get(@PathVariable("id") Long id) {
        return new Result<BusiOrderDetailVo>().ok(busiOrderService.detail(id));
    }

    @Login
    @PutMapping
    @ApiOperation("发货")
    public Result deliver(@RequestBody @Validated BusiOrderDeliverDTO dto) {
        //效验数据
        busiOrderService.deliver(dto);
        return new Result();
    }


}
